﻿using ConsoleApp2.Enum;
using ConsoleApp2.Impl;
using ConsoleApp2.Service;
using ConsoleApp2.Models;
using ConsoleApp2.User;

namespace ConsoleApp2.Utils;

public class SystemUtils
{
    private UserLoginService userLoginService { get; set; }
    private CodeService CodeService { get; set; }
    private UserRegisterService userRegisterService { get; set; }
    private LocalUserSystemService localUserSystemService { get; set; }
    private static int USER_INDEX_COUNT = 0;
    private static List<ConsoleApp2.Models.User> users = new List<ConsoleApp2.Models.User>();
    private PermissionService PermissionService { get; set; }

    public SystemUtils()
    {
        userLoginService = new UserLoginServiceImpl();
        CodeService = new CodeServiceImpl();
        localUserSystemService = new LocalUserSystemServiceImpl();
        userRegisterService = new UserRegisterServiceImpl();
        PermissionService = new PermissionServiceImpl();
    }

    public void LoginRegisterChose()
    {
        Console.BackgroundColor = ConsoleColor.Cyan;
        Console.WriteLine("欢迎来到 信仰 后台管理系统");
    newLoginOrRegister:
        Console.WriteLine("请选择登入还是注册 按1登入 按2注册 按q退出");
        Console.BackgroundColor = ConsoleColor.Black;
        string userLoginChose = Console.ReadLine();
        while (true)
        {
            if (userLoginChose == "1")
            {
            newLogin:
                Console.WriteLine("请选择登入方式 1.账号登入 2.邮箱登入 3.手机号登入");
                string userChose = Console.ReadLine();
                ConsoleApp2.Models.User user = new ConsoleApp2.Models.User();
                userChose = userLoginService.BeforeLogin(userChose);
                bool IsLogin = UserChoseLogin(userChose, ref user);
                if (IsLogin)
                {
                    Console.WriteLine("登入成功");
                    RoleManagerUtil roleManagerUtil = new RoleManagerUtil();
                    roleManagerUtil.LoginAfterRoleManger(user);
                }
                else
                {
                    Console.WriteLine("登入失败，请重新登入");
                    goto newLogin;
                }
                return;
            }
            else if (userLoginChose == "2")
            {
                ConsoleApp2.Models.User user = RegisterInfo();
                goto newLoginOrRegister;
                return;
            }
            else if (userLoginChose == "q")
            {
                Console.WriteLine("正在退出程序");
                return;
            }
            else
            {
                Console.WriteLine("输入有误 请重新输入 ---》 按1选择登入 按2选择注册 按q选择退出");
                userLoginChose = Console.ReadLine();
            }
        }
    }

    private ConsoleApp2.Models.User RegisterInfo()
    {
        ConsoleApp2.Models.User user = new ConsoleApp2.Models.User();
        user.CreateTime = DateTime.Now;
        user.UpdateTime = DateTime.Now;

        Console.WriteLine("输入匿名");
        user.NickName = Console.ReadLine();
        user.NickName = UserInputUtils.CheckUserInputLength(user.NickName, "匿名");

        Console.WriteLine("请输入账号");
        user.UserName = Console.ReadLine();
        user.UserName = UserInputUtils.CheckUserInputLength(user.UserName, "账号");
        user.UserName = CheckUtils.CheckUserRepeat(user.UserName);

        Console.WriteLine("请输入密码");
        user.Password = Console.ReadLine();
        user.Password = UserInputUtils.CheckUserInputLength(user.Password, "密码");
        user.Password = Encory.EncryPwd(user.Password);

        Console.WriteLine("请输入邮箱");
        user.Email = Console.ReadLine();
        while (!CheckUtils.IsEmail(user.Email))
        {
            Console.WriteLine("邮箱格式不正确，请重新输入");
            user.Email = Console.ReadLine();
        }
        user.Email = UserInputUtils.CheckUserInputLength(user.Email, "邮箱");

        Console.WriteLine("正在发送邮箱验证码");
        System.Threading.Thread.Sleep(2000);
        Console.WriteLine("邮箱验证码发送完成，请输入验证码");

        Code code = CodeService.CreateCode();
        Console.WriteLine("验证码：" + code.OldCode);
        code.UserInputCode = Console.ReadLine();
        while (!CodeService.CheckCode(code))
        {
            Console.WriteLine("验证码错误，请重新输入");
            code.UserInputCode = Console.ReadLine();
        }
        Console.WriteLine("邮箱验证码校验成功");

        // 为新用户分配普通用户角色
        RoleService roleService = new RoleServiceImpl();
        List<Role> roles = roleService.GetAllRoles();
        user.Roles = new List<Role>() { roles.First(r => r.RoleName == RoleName.普通用户) };

        // 保存新用户到 LocalUserSystemService
        localUserSystemService.SaveUser(user);
        USER_INDEX_COUNT++;
        Console.WriteLine("注册成功");

        return user;
    }

    private bool UserChoseLogin(string userChose, ref ConsoleApp2.Models.User user)
    {
        bool IsLogin = false;
        user = new ConsoleApp2.Models.User();
        if (userChose == "1")
        {
        newLogin:
            Console.WriteLine("请输入账号");
            user.UserName = Console.ReadLine();
            user.UserName = UserInputUtils.CheckUserInputLength(user.UserName, "账号");

            LoginAfter(ref user);
            user = userLoginService.UserNameLogin(user);
        }
        else if (userChose == "2")
        {
            Console.WriteLine("请输入邮箱");
            user.Email = Console.ReadLine();
            while (!CheckUtils.IsEmail(user.Email))
            {
                Console.WriteLine("请重新输入邮箱");
                user.Email = Console.ReadLine();
            }

            user.Email = UserInputUtils.CheckUserInputLength(user.Email, "邮箱");

            LoginAfter(ref user);
            user = userLoginService.EmailLogin(user);
        }
        else
        {
            Console.WriteLine("请输入手机号");
            user.Phone = Console.ReadLine();
            while (!CheckUtils.IsPhone(user.Phone))
            {
                Console.WriteLine("格式不正确，请重新输入手机号");
                user.Phone = Console.ReadLine();
            }

            user.Phone = UserInputUtils.CheckUserInputLength(user.Phone, "手机号");
            LoginAfter(ref user);
            user = userLoginService.PhoneLogin(user);
        }

        return user != null;
    }

    public void LoginAfter(ref ConsoleApp2.Models.User user)
    {
        Console.WriteLine("请输入密码");
        user.Password = Console.ReadLine();
        user.Password = UserInputUtils.CheckUserInputLength(user.Password, "密码");
        user.Password = Encory.EncryPwd(user.Password);

    newCode:
        Code systemCode = CodeService.CreateCode();
    nextCode:
        Console.WriteLine("请输入验证码： " + systemCode.OldCode);
        systemCode.UserInputCode = Console.ReadLine();
        bool isOut = CodeService.CheckCodeExpiration(systemCode);
        if (!isOut)
        {
            Console.WriteLine("验证码已经过期了 正在重新生成");
            System.Threading.Thread.Sleep(3000);
            goto newCode;
        }
        bool isCode = CodeService.CheckCode(systemCode);
        while (!isCode)
        {
            Console.WriteLine("验证码输入错误，请重新输入");
            goto nextCode;
        }
        Console.WriteLine("验证码校验成功");
    }
}